//
//  SettingVC.swift
//  com.yunshu.yunshuapp
//
//  Created by 周文杰 on 2017/6/18.
//  Copyright © 2017年 zwj. All rights reserved.
//

import UIKit

class SettingVC: UIViewController {

    let contentView = UIScrollView.init(frame: CGRect.init(x: 0, y: 0, width: Const.SCREEN_WIDTH, height: Const.SCREEN_HEIGHT))
    let logoVC = UIImageView.init(frame: CGRect.init(x: 100, y: 20, width: Const.SCREEN_WIDTH - 200 , height: Const.SCREEN_WIDTH - 200))
    let productLabel = UILabel.init(frame: CGRect.init(x: 15, y: 10, width: Const.SCREEN_WIDTH - 30, height: 50))
    
    let TEL_400 = "15383800066"
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        contentView.backgroundColor = self.view.backgroundColor
        self.view.addSubview(contentView)

        logoVC.image = UIImage.init(named: "logo")
        contentView.addSubview(logoVC)
        productLabel.text = "云数智惠" + (Bundle.main.infoDictionary?["CFBundleShortVersionString"] as! String)
        productLabel.textAlignment = .center
        productLabel.tag = 99
        contentView.addSubview(productLabel)
        
        self.addItem("服务条款").addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(SettingVC.agreement)))
        
        self.addItem("隐私政策").addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(SettingVC.privacy)))
        
        self.addItem("意见反馈").addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(SettingVC.feedback)))
        
        self.addItem("联系客服",subtitle:TEL_400).addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(SettingVC.dial)))
    }

    
    @objc func agreement() {
        let vc:WebViewController = UIStoryboard.init(name: "mainStoryBoard", bundle: Bundle.main).instantiateViewController(withIdentifier: WebViewController.VCID) as! WebViewController
        vc.url = NetworkManager.BASE_URL + NetworkManager.AGREEMENT
        self.navigationController?.pushViewController(vc)

    }
    
    @objc func privacy() {
        let vc:WebViewController = UIStoryboard.init(name: "mainStoryBoard", bundle: Bundle.main).instantiateViewController(withIdentifier: WebViewController.VCID) as! WebViewController
        vc.url = NetworkManager.BASE_URL + NetworkManager.PRIVACY
        self.navigationController?.pushViewController(vc)

    }
    
    @objc func dial() {
        let str="telprompt://" + TEL_400
        // NSLog(@"str======%@",str);
        UIApplication.shared.openURL(URL.init(string: str)!)
    }
    
    @objc func feedback() {
        
        let vc:FeedbackVC = UIStoryboard.init(name: "mainStoryBoard", bundle: Bundle.main).instantiateViewController(withIdentifier: "FeedbackVC") as! FeedbackVC
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    func addItem(_ name : String , subtitle:String? = nil ) ->UIView{
        let container = UIView.init(frame: CGRect.init(x: 0, y: 0, width: Const.SCREEN_WIDTH, height: 52))
        container.backgroundColor = UIColor.white
        container.tag = 99
        let titleLabel = UILabel.init(frame: CGRect.init(x: 15, y: 0, width: 100, height: 52))
        titleLabel.text = name
        container.addSubview(titleLabel)
        
        let line = UIView.init(frame: CGRect.init(x: 10, y: 52 - Const.ONE_PIXEL, width: Const.SCREEN_WIDTH - 20, height: Const.ONE_PIXEL))
        line.backgroundColor = Const.WTM_LIGHTGRAY
        container.addSubview(line)
        
        let arrow = UIImageView.init(frame: CGRect.init(x: Const.SCREEN_WIDTH - 40 , y: 11, width: 30, height: 30))
        arrow.image = UIImage.init(named: "right_arrow")
        container.addSubview(arrow)
        
        if let title2 = subtitle {
            let l = UILabel.init(frame: CGRect.init(x: Const.SCREEN_WIDTH - 200, y: 0, width: 160, height: 52))
            l.text = title2
            l.textAlignment = .right
            l.textColor = Const.WTM_GRAY
            container.addSubview(l)
        }
        
        self.contentView.addSubview(container)
        return container
    }

    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        var y:CGFloat = Const.SCREEN_WIDTH - 100 + 30
        for iView in contentView.subviews {
            if iView.tag == 99 {
                var frame = iView.frame
                frame.origin.y = y
                y += frame.size.height
                iView.frame = frame
            }
        }
        
        self.contentView.contentSize = CGSize.init(width: Const.SCREEN_WIDTH, height: y + 20)
    }


}
